package com.quick.develop.flowable.service.task.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.quick.develop.flowable.constant.BpmConstants;
import com.quick.develop.flowable.mapper.BpmProcessInstanceMapper;
import com.quick.develop.flowable.service.task.IBpmExecutionService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.runtime.Execution;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author junyuan.liu
 * @description: TODO
 * @date 2022/6/1 19:16
 */
@Service
@Slf4j
@Validated
public class BpmExecutionServiceImpl implements IBpmExecutionService {
    @Resource
    private RuntimeService runtimeService;
    
    @Resource
    private BpmProcessInstanceMapper bpmProcessInstanceMapper;
    
    @Override
    public Execution getExecutionById(String id) {
        return runtimeService.createExecutionQuery().executionId(id).singleResult();
    }

    @Override
    public List<Execution> getExecutionByParentId(String id) {
        return runtimeService.createExecutionQuery().parentId(id).list();
    }

    @Override
    public String getSubStartUser(String executionId) {
        List<Map<String, String>> list = bpmProcessInstanceMapper
                .getRuVariableStr(BpmConstants.SUB_PROCESS_START_USER, executionId);
        return CollectionUtil.isNotEmpty(list) ? list.get(0).get("text") : null;
    }
}
